package com.lsfb.signln.Location.LocationSearch;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.lsfb.signln.Location.LocationSearch.ReplenishSignIn.ReplenishSignInActivity;
import com.lsfb.signln.Login.LoginActivity;
import com.lsfb.signln.R;
import com.lsfb.signln.Utils.LittleUtils;
import com.lsfb.signln.Utils.LsfbUtils;
import com.lsfb.signln.Utils.URLString;
import com.lsfb.signln.View.RecycleViewDivider;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.adapter.DividerItemDecoration;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.DensityUtils;
import com.zgscwjm.lsfbbasetemplate.utils.T;
import com.zgscwjm.lsfbbasetemplate.view.titlebar.LsfbTitleBar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;


/**
 * Created by Administrator on 2018/1/24 0024.
 */
@ContentView(R.layout.aty_location_search)
public class LocationSearchActivity extends LsfbActivity implements CalendarView.OnDateSelectedListener,
        CalendarView.OnMonthChangeListener,
        CalendarView.OnYearChangeListener,
        CalendarView.OnDateLongClickListener, HorizontalTimerAdapter.ItemCheckedListener {

    /**
     * 日历
     */
    @ViewInject(R.id.calendarView)
    private CalendarView mCalendarView;

    /**
     * 月外勤定位数
     */
    @ViewInject(R.id.aty_record_text_out)
    private TextView text_out;

    /**
     * 内勤定位数
     */
    @ViewInject(R.id.aty_record_text_init)
    private TextView text_init;

    /**
     * 漏签数
     */
    @ViewInject(R.id.aty_record_text_seep)
    private TextView text_seep;

    /**
     * 请假数
     */
    @ViewInject(R.id.aty_record_text_singin)
    private TextView text_singin;

    /**
     * 选中日期
     */
    @ViewInject(R.id.fg_home_text_time)
    private TextView tv_curDate;

    /**
     * 横向选择器
     */
    @ViewInject(R.id.horizontal_review)
    private RecyclerView horizontal_review;
    private HorizontalTimerAdapter timerAdapter;
    private List<String> dates;
    private int mYear;
    private int mMonth;
    private String date;

    /**
     * 数据列表
     */
    @ViewInject(R.id.review)
    private RecyclerView review;
    private LocationSearchAdapter adapter;
    private List<LocationSearchBean.CurrentDayDetailsBean.ListBean> datas;

    @Override

    public void initView() {
        LsfbTitleBar titleBar = LittleUtils.setsimpletitlebar(this, "定位查询");
        TextView textView = LittleUtils.rightbtn(this, "补录");

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LocationSearchActivity.this, ReplenishSignInActivity.class));
            }
        });
        titleBar.setRightView(textView, 0, 0, DensityUtils.dp2px(this, 10), 0);

    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {
        dates = new ArrayList<>();
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        mYear = calendar.get(java.util.Calendar.YEAR);
        mMonth = calendar.get(java.util.Calendar.MONTH) + 1;
        for (int i = 10; i > 0; i--) {
            date = String.valueOf(mYear) + "-" + String.valueOf(mMonth);
            dates.add(date);
            if (mMonth == 1) {
                mMonth = 12;
                mYear = mYear - 1;
            } else {
                mMonth = mMonth - 1;
            }

        }
        timerAdapter = new HorizontalTimerAdapter(this, dates, R.layout.item_hyx_dates, this);
        LinearLayoutManager layout = new LinearLayoutManager(this);
        layout.setOrientation(LinearLayoutManager.HORIZONTAL);
        RecycleViewDivider di = LsfbUtils.getHORIZONTALLineDivider(this, R.color.white_color, 8);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, LinearLayoutManager.HORIZONTAL);
        layout.setStackFromEnd(true);//列表再底部开始展示，反转后由上面开始展示
        layout.setReverseLayout(true);//列表翻转
        horizontal_review.setLayoutManager(layout);
        horizontal_review.addItemDecoration(di);
        horizontal_review.setAdapter(timerAdapter);     //绑定adapter
        horizontal_review.smoothScrollToPosition(0);    //定位到指定位置


        mCalendarView.setOnYearChangeListener(this);
        mCalendarView.setOnDateSelectedListener(this);
        mCalendarView.setOnMonthChangeListener(this);
        mCalendarView.setOnDateLongClickListener(this);
        mCalendarView.scrollToNext(false);
        mCalendarView.scrollToPre(false);

        //滚动到指定日期
        mCalendarView.scrollToCalendar(mCalendarView.getCurYear(), mCalendarView.getCurMonth(), mCalendarView.getCurDay());

        datas = new ArrayList<>();
        adapter = new LocationSearchAdapter(this, datas, R.layout.item_signln);
        review.setLayoutManager(new LinearLayoutManager(this));
//        review.addItemDecoration(LsfbUtils.getVERTICALLineDivider(this, R.color.white_color, 10));
        review.setAdapter(adapter);

        /**
         * 获取数据
         */
        getData(mCalendarView.getCurYear(), mCalendarView.getCurMonth(), java.util.Calendar.getInstance(Locale.CHINA).getActualMaximum(java.util.Calendar.DATE));

        /**
         * 获取今日数据
         */
        getCurDayData(mCalendarView.getCurYear(), mCalendarView.getCurMonth(), mCalendarView.getCurDay());
    }

    /**
     * 获取数据
     */
    private void getData(int year, int month, int day) {

        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("year", String.valueOf(year));
        hashMap.put("month", String.valueOf(month - 1));
        hashMap.put("days", String.valueOf(day));
        LocationSearchEvent<LocationSearchBean> event = new LocationSearchEvent<>();
        event.setDay(day);
        event.setMonth(month);
        event.setYear(year);
        new BaseInternet().getData(URLString.SIGNCALENDAR, hashMap, LocationSearchBean.class, event, true);
    }

    @EventAnnotation
    public void LocationSearchEvent(LocationSearchEvent<LocationSearchBean> event) {
        switch (event.getCode()) {
            case 200:
                text_out.setText(event.getData().getOutside());
                text_init.setText(event.getData().getInternal());
                text_seep.setText(event.getData().getLeakage());
                text_singin.setText(event.getData().getLeave());
                //当月天数的详情,这里直接传31条数据进来,0是默认状态,1 为内勤 状态 2 为正常外勤 3 为外勤补签 状态 4 为请假 状态 5 为漏签 状态
                final List<Calendar> schemes = new ArrayList<>();
                for (int i = 0; i < event.getData().getDays().size(); i++) {
                    if (event.getData().getDays().get(i).equals("1")) {
                        schemes.add(getSchemeCalendar(event.getYear(), event.getMonth(), i + 1, Color.parseColor("#0EAE6E"), ""));
                    } else if (event.getData().getDays().get(i).equals("2")) {
                        schemes.add(getSchemeCalendar(event.getYear(), event.getMonth(), i + 1, Color.parseColor("#5094F3"), ""));
                    } else if (event.getData().getDays().get(i).equals("3")) {
                        schemes.add(getSchemeCalendar(event.getYear(), event.getMonth(), i + 1, Color.parseColor("#5094F3"), "补"));
                    } else if (event.getData().getDays().get(i).equals("4")) {
                        schemes.add(getSchemeCalendar(event.getYear(), event.getMonth(), i + 1, Color.parseColor("#F63F3F"), ""));
                    } else if (event.getData().getDays().get(i).equals("5")) {
                        schemes.add(getSchemeCalendar(event.getYear(), event.getMonth(), i + 1, Color.parseColor("#32B1D2"), ""));
                    }
                }
                mCalendarView.setSchemeDate(schemes);

                //签到记录
                if (event.getData().getCurrentDayDetails().getList() != null && !event.getData().getCurrentDayDetails().getList().isEmpty()) {
                    datas.clear();
                    datas.addAll(event.getData().getCurrentDayDetails().getList());
                    adapter.notifyDataSetChanged();
                } else {
                    datas.clear();
                    adapter.notifyDataSetChanged();
                }
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    private Calendar getSchemeCalendar(int year, int month, int day, int color, String text) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.setSchemeColor(color);//如果单独标记颜色、则会使用这个颜色
        calendar.setScheme(text);
        return calendar;
    }

    /**
     * 长按某个日期
     *
     * @param calendar
     */
    @Override
    public void onDateLongClick(Calendar calendar) {

    }

    /**
     * 点击某个日期
     *
     * @param calendar
     * @param isClick
     */
    @Override
    public void onDateSelected(Calendar calendar, boolean isClick) {
        if (isClick) {
            getCurDayData(calendar.getYear(), calendar.getMonth(), calendar.getDay());
        }
    }

    private void getCurDayData(int year, int month, int day) {
        tv_curDate.setText(String.valueOf(month) + "月" + String.valueOf(day) + "日");
        HashMap hashMap = new HashMap();
        hashMap.put("year", String.valueOf(year));
        hashMap.put("month", String.valueOf(month - 1));
        hashMap.put("days", String.valueOf(day));
        new BaseInternet().getData(URLString.CURSIGNDETAILS, hashMap, LocationSearchBean.CurrentDayDetailsBean.class, new CurDateSignEvent<LocationSearchBean.CurrentDayDetailsBean>(), true);
    }

    @EventAnnotation
    public void eventResult(CurDateSignEvent<LocationSearchBean.CurrentDayDetailsBean> event) {
        switch (event.getCode()) {
            case 200:
                if (event.getData().getList() != null && !event.getData().getList().isEmpty() && event.getData().getList().size() != 0) {
                    datas.clear();
                    datas.addAll(event.getData().getList());
                    adapter.notifyDataSetChanged();
                } else {
                    datas.clear();
                    adapter.notifyDataSetChanged();
                }
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    /**
     * 月份改变
     *
     * @param year
     * @param month
     */
    @Override
    public void onMonthChange(int year, int month) {

    }

    /**
     * 年改变
     *
     * @param year
     */
    @Override
    public void onYearChange(int year) {

    }

    /**
     * 横向日期选择
     *
     * @param model
     */
    @Override
    public void onChecked(String model, int position) {
        horizontal_review.smoothScrollToPosition(position);
        timerAdapter.notifyDataSetChanged();
        String year = model.substring(0, 4);
        String month = model.substring(5, model.length());
        try {
            int y = Integer.parseInt(year);
            int m = Integer.parseInt(month);
            //滚动到指定日期
            mCalendarView.scrollToCalendar(y, m, 1);

            java.util.Calendar a = java.util.Calendar.getInstance();
            a.set(java.util.Calendar.YEAR, y);
            a.set(java.util.Calendar.MONTH, m - 1);
            a.set(java.util.Calendar.DATE, 1);
            a.roll(java.util.Calendar.DATE, -1);
            int maxDate = a.get(java.util.Calendar.DATE);

            getData(y, m, maxDate);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

    }
}
